/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeu.edu.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
import upeu.edu.pe.model.SauEstandar;
import upeu.edu.pe.model.SauEstandarEvaluacion;
import upeu.edu.servicio.EstandarServicio;

/**
 *
 * @author pc
 */
@Controller
public class EstandarController {
    @Autowired
    public EstandarServicio estandarServicio;
    Log log=LogFactory.getLog(EstandarController.class);
    
    @RequestMapping(value="eliminarEst",method= RequestMethod.GET)
    public ModelAndView eliminarEstandar(HttpServletRequest request){
        String idEstandar =request.getParameter("idEstandarE")==null?"":request.getParameter("idEstandarE");
        estandarServicio.eliminarEstandar(idEstandar);
        return new ModelAndView("redirect:reporte.dmp");
    }
    
     @RequestMapping(value="reporte", method= RequestMethod.GET) 
   public ModelAndView irReporte(){         
       List<SauEstandar> lista=estandarServicio.listaEstandar();       
      
       Map<String, Object> modelo=new HashMap<String, Object>();
       modelo.put("listEstandar", lista);
       modelo.put("msg", "Reporte de Estandar");
        return new ModelAndView("modulos/estandar/mainEstandar", modelo);
    }
     
@RequestMapping(value="estandarGuardar", method= RequestMethod.POST)
   public ModelAndView guardarEstandar(@ModelAttribute("ModeloEstandar") SauEstandar estandar, BindingResult result) {  
       estandarServicio.insertarEstandar(estandar);
   return new ModelAndView("index");

   }

@RequestMapping(value="formEstandar", method= RequestMethod.GET)
public ModelAndView irFormulario(@ModelAttribute("ModeloEstandar") SauEstandar estandar, BindingResult result){
    return new ModelAndView("modulos/estandar/formPeriodo");
}

@RequestMapping(value="editarEstandar", method= RequestMethod.GET)
public ModelAndView editarEstandarForm(HttpServletRequest request){
String idEstandar=request.getParameter("idEstandarE")==null?"":request.getParameter("idEstandarE");
SauEstandar estandarTO=null;
estandarTO=estandarServicio.buscarEstandarId(idEstandar);
return new ModelAndView("modulos/estandar/formEditPeriodo", "ActualizarModelo",estandarTO);
}

@RequestMapping(value="actualizarEstandar", method= RequestMethod.POST)
public ModelAndView actualizarEstandar(@ModelAttribute("ActualizarEstandar") SauEstandar estandar, BindingResult result){
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String fechaRecepcionada=String.valueOf(estandar.getFecha());
System.out.println(fechaRecepcionada);
    try {
        estandar.setFecha(format.parse(fechaRecepcionada));
    } catch (Exception ex) {
        Logger.getLogger(EstandarController.class.getName()).log(Level.SEVERE,null,ex);
    }
    estandarServicio.actualizarEstandar(estandar);
    return new ModelAndView("redirect:reporte.dmp");
}

}
